/**
 * 问题管理页面js
 * 版权所有：上海戈品投资管理有限公司
 *   author king
     功能说明：问题管理
     创建日期：2015-12-14
     修改记录：
     备注说明：
 */

$(document).ready(function() {
	answerList();
});

/**
 * 查询答案
 * @returns
 */
var answerList = function(){

	var questionId = $('#questionId').val();
	var datas =  {"p":"questionManage","m":"queryAnswerList","questionId":questionId};
	var columnDefine = {"id":"编号","questionId":"问题ID","remark":"备注","content":"答案","isAnswer":"是否正确答案"};
	
	var formatters={
            "isAnswer": function (column, rowData)
            {
            	if( rowData.isAnswer == 1 )
            	{
            		return '非正确答案';
            	}	
            	else
            	{
            		return '正确答案';
            	}	
            }
	 };
	 var buttons =''; //操作按钮
		buttons+='<p>';
		buttons+='<button type=\'button\' class=\'btn btn-w-m btn-primary\' onclick="openModifyWin();">修改选项</button>&nbsp;&nbsp;';
		buttons+='<button type=\'button\' class=\'btn btn-w-m btn-danger\' onclick="removeAnswer();">删除答案</button>&nbsp;&nbsp;';
		buttons+='</p>';
	   hui.pager('service',datas ,"dataList", columnDefine , formatters , buttons ); //分页查询
};


/**
 * 显示修改窗口
 * @returns
 */
var openModifyWin = function(){
	var data = $('#pop_win .data').text();
	var json = eval('('+data+')');
	$('#modifyAnswerWin input').val('');
	$('#modifyAnswerWin .id').val(json.id);
	$('#modifyAnswerWin .content').val(json.content);
	$('#modifyAnswerWin .isAnswer').val(json.isAnswer);
	$('#pop_win').modal('hide');
	$('#modifyAnswerWin').modal('show');
};

/**
 * 显示新建窗口
 * @returns
 */
var openNewWin  = function(){
	$('#newAnswerWin input').val('');
	$('#pop_win').modal('hide');
	$('#newAnswerWin').modal('show');
};


/**
 * 新建答案
 * @returns
 */
var add = function(){
	var questionId = $('#questionId').val();
	var content = $('#newAnswerWin .content').val();
	var isAnswer = $('#newAnswerWin .isAnswer').val();
	var datas = {"p":"questionManage","m":"addAnswer","questionId":questionId,"content":content,"isAnswer":isAnswer};
	var json = common.ajaxRequest('service', datas, 'POST');
	
	if( json && json.result == 0 )
    {
		answerList();
		$('#newAnswerWin').modal('hide');
		common.showMsg('info', null , '创建成功!');
    }
	else
	{
		common.showMsg('error', null , '创建失败!');
		return;
	}
};


/**
 * 修改答案
 * @returns
 */
var modify = function(){
	var id = $('#modifyAnswerWin .id').val();
	var content = $('#modifyAnswerWin .content').val();
	var isAnswer = $('#modifyAnswerWin .isAnswer').val();
	var datas = {"p":"questionManage","m":"modifyAnswer","id":id,"content":content,"isAnswer":isAnswer};
	var json = common.ajaxRequest('service', datas, 'POST');
	
	if( json && json.result == 0 )
	{
		answerList();
		$('#modifyAnswerWin').modal('hide');
		common.showMsg('info', null , '修改成功!');
	}
	else
	{
		common.showMsg('error', null , '修改失败!');
		return;
	}
};


/**
 * 删除答案
 * @returns
 */
var removeAnswer = function(){
	var data = $('#pop_win .data').text();
	var json = eval('('+data+')');
	
	var datas = {"p":"questionManage","m":"deleteAnswer","id":json.id};
	var json = common.ajaxRequest('service', datas, 'POST');
	
	if( json && json.result == 0 )
	{
		answerList();
		$('#pop_win').modal('hide');
		common.showMsg('info', null , '删除成功!');
	}
	else
	{
		common.showMsg('error', null , '删除失败!');
		return;
	}
};